package org.example.petsystem.controller;

import org.example.petsystem.entity.ResultBody;
import org.example.petsystem.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Post")
@CrossOrigin(origins="*")
public class PostController {

    @Autowired
    private PostService postService;

    //删除帖子
    @PostMapping("/deletePost")
    public ResultBody deletePost(@RequestBody Map<String,Object> map) {
        System.out.println("删除帖子");
        //判断参数是否完整
        if(map.get("code") == null || map.get("phone") == null || 
           map.get("author") == null || map.get("title") == null) {
            return new ResultBody(400, "参数不完整", false);
        }
        return postService.deletePost(map);
    }
    
    //对帖子进行编辑,并返回结果
    @RequestMapping("/sendPost")
    public ResultBody sendPost(@RequestBody Map<String,Object> map){
        //判断参数是否完整
        if(map.get("avatar")==null||map.get("author")==null||map.get("content")==null||map.get("phone")==null||map.get("code")==null||map.get("title")==null)
        {
            return new ResultBody(404,"参数不完整",false);
        }
        //参数核实完毕，将参数传入下一层
        return postService.sendPost(map);
    }

    //查看所有帖子
    @RequestMapping("/displayPost")
    public ResultBody displayPost(){
        return postService.displayPost();
    }

    //搜索帖子
    @RequestMapping("/searchPost")
    public ResultBody searchPost(@RequestBody Map<String,Object> map){
        //判断参数是否完整
        if(map.get("searchcontent")==null)
        {
            return new ResultBody(404,"参数不完整",false);
        }
        //参数核实完毕，将参数传入下一层
        return postService.searchPost(map);
    }

    //发送评论
    @RequestMapping("/sendComment")
    public ResultBody sendComment(@RequestBody Map<String,Object> map){
        //判断参数是否完整
        if(map.get("code")==null||map.get("comment")==null||map.get("phone")==null)
        {
            return new ResultBody(404,"参数不完整",false);
        }
        //参数核实完毕，将参数传入下一层
        return postService.sendComment(map);
    }

    @RequestMapping("/displayComment")
    public ResultBody displayComment(@RequestBody Map<String,Object> map){
        //判断参数是否完整
        if(map.get("code")==null)
        {
            return new ResultBody(404,"参数不完整",false);
        }
        //参数核实完毕，将参数传入下一层
        return postService.displayComment(map);
    }

    // 新增的帖子管理接口
    @PostMapping("/list")
    public ResultBody getPostList() {
        try {
            List<Map<String,Object>> posts = postService.getPostList();
            return new ResultBody(200, "success", posts, posts.size());
        } catch (Exception e) {
            return new ResultBody(500, "获取帖子列表失败: " + e.getMessage());
        }
    }

    @PostMapping("/approve")
    public ResultBody approvePost(@RequestBody Map<String,Object> params) {
        try {
            Integer postId = (Integer) params.get("postId");
            if (postId == null) {
                return new ResultBody(400, "帖子ID不能为空");
            }
            
            boolean success = postService.approvePost(postId);
            if (success) {
                return new ResultBody(200, "success");
            } else {
                return new ResultBody(404, "帖子不存在");
            }
        } catch (Exception e) {
            return new ResultBody(500, "审核帖子失败: " + e.getMessage());
        }
    }

    @PostMapping("/reject")
    public ResultBody rejectPost(@RequestBody Map<String,Object> params) {
        try {
            Integer postId = (Integer) params.get("postId");
            String reason = (String) params.get("reason");
            
            if (postId == null) {
                return new ResultBody(400, "帖子ID不能为空");
            }
            if (reason == null || reason.trim().isEmpty()) {
                return new ResultBody(400, "拒绝原因不能为空");
            }
            
            boolean success = postService.rejectPost(postId, reason);
            if (success) {
                return new ResultBody(200, "success");
            } else {
                return new ResultBody(404, "帖子不存在");
            }
        } catch (Exception e) {
            return new ResultBody(500, "拒绝帖子失败: " + e.getMessage());
        }
    }

    // 获取用户帖子列表
    @PostMapping("/userPosts")
    public ResultBody getUserPosts(@RequestBody Map<String,Object> params) {
        try {
            String phone = (String) params.get("phone");
            if (phone == null || phone.trim().isEmpty()) {
                return new ResultBody(400, "手机号不能为空");
            }
            return postService.getUserPosts(phone);
        } catch (Exception e) {
            return new ResultBody(500, "获取用户帖子列表失败: " + e.getMessage());
        }
    }
}
